<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>colorpicker.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_colorpicker.js.html">colorpicker.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A color picker component.  A color picker can compose several
<a name="line17"></a> * instances of goog.ui.ColorPalette.
<a name="line18"></a> *
<a name="line19"></a> * NOTE: The ColorPicker is in a state of transition towards the common
<a name="line20"></a> * component/control/container interface we are developing.  If the API changes
<a name="line21"></a> * we will do our best to update your code.  The end result will be that a
<a name="line22"></a> * color picker will compose multiple color palettes.  In the simple case this
<a name="line23"></a> * will be one grid, but may consistute 3 distinct grids, a custom color picker
<a name="line24"></a> * or even a color wheel.
<a name="line25"></a> *
<a name="line26"></a> */
<a name="line27"></a>
<a name="line28"></a>goog.provide(&#39;goog.ui.ColorPicker&#39;);
<a name="line29"></a>goog.provide(&#39;goog.ui.ColorPicker.EventType&#39;);
<a name="line30"></a>
<a name="line31"></a>goog.require(&#39;goog.color&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.ColorPalette&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line34"></a>goog.require(&#39;goog.ui.Component.State&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Create a new, empty color picker.
<a name="line39"></a> *
<a name="line40"></a> * @param {goog.dom.DomHelper} opt_domHelper Optional DOM helper.
<a name="line41"></a> * @param {goog.ui.ColorPalette} opt_colorPalette Optional color palette to
<a name="line42"></a> *     use for this color picker.
<a name="line43"></a> * @extends {goog.ui.Component}
<a name="line44"></a> * @constructor
<a name="line45"></a> */
<a name="line46"></a>goog.ui.ColorPicker = function(opt_domHelper, opt_colorPalette) {
<a name="line47"></a>  goog.ui.Component.call(this, opt_domHelper);
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * The color palette used inside the color picker.
<a name="line51"></a>   * @type {goog.ui.ColorPalette?}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.colorPalette_ = opt_colorPalette || null;
<a name="line55"></a>
<a name="line56"></a>  this.getHandler().listen(
<a name="line57"></a>      this, goog.ui.Component.EventType.ACTION, this.onColorPaletteAction_);
<a name="line58"></a>};
<a name="line59"></a>goog.inherits(goog.ui.ColorPicker, goog.ui.Component);
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * Default number of columns in the color palette. May be overridden by calling
<a name="line64"></a> * setSize.
<a name="line65"></a> *
<a name="line66"></a> * @type {number}
<a name="line67"></a> */
<a name="line68"></a>goog.ui.ColorPicker.DEFAULT_NUM_COLS = 5;
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Constants for event names.
<a name="line73"></a> * @enum {string}
<a name="line74"></a> */
<a name="line75"></a>goog.ui.ColorPicker.EventType = {
<a name="line76"></a>  CHANGE: &#39;change&#39;
<a name="line77"></a>};
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Whether the component is focusable.
<a name="line82"></a> * @type {boolean}
<a name="line83"></a> * @private
<a name="line84"></a> */
<a name="line85"></a>goog.ui.ColorPicker.prototype.focusable_ = true;
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * Gets the array of colors displayed by the color picker.
<a name="line90"></a> * Modifying this array will lead to unexpected behavior.
<a name="line91"></a> * @return {Array.&lt;string&gt;?} The colors displayed by this widget.
<a name="line92"></a> */
<a name="line93"></a>goog.ui.ColorPicker.prototype.getColors = function() {
<a name="line94"></a>  return this.colorPalette_ ? this.colorPalette_.getColors() : null;
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Sets the array of colors to be displayed by the color picker.
<a name="line100"></a> * @param {Array.&lt;string&gt;} colors The array of colors to be added.
<a name="line101"></a> */
<a name="line102"></a>goog.ui.ColorPicker.prototype.setColors = function(colors) {
<a name="line103"></a>  // TODO: Don&#39;t add colors directly, we should add palettes and the
<a name="line104"></a>  // picker should support multiple palettes.
<a name="line105"></a>  if (!this.colorPalette_) {
<a name="line106"></a>    this.createColorPalette_(colors);
<a name="line107"></a>  } else {
<a name="line108"></a>    this.colorPalette_.setColors(colors);
<a name="line109"></a>  }
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Sets the array of colors to be displayed by the color picker.
<a name="line115"></a> * @param {Array.&lt;string&gt;} colors The array of colors to be added.
<a name="line116"></a> * @deprecated Use setColors.
<a name="line117"></a> */
<a name="line118"></a>goog.ui.ColorPicker.prototype.addColors = function(colors) {
<a name="line119"></a>  this.setColors(colors);
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Sets the size of the palette.  Will throw an error after the picker has been
<a name="line125"></a> * rendered.
<a name="line126"></a> * @param {goog.math.Size|number} size The size of the grid.
<a name="line127"></a> */
<a name="line128"></a>goog.ui.ColorPicker.prototype.setSize = function(size) {
<a name="line129"></a>  // TODO: The color picker should contain multiple palettes which will
<a name="line130"></a>  // all be resized at this point.
<a name="line131"></a>  if (!this.colorPalette_) {
<a name="line132"></a>    this.createColorPalette_([]);
<a name="line133"></a>  }
<a name="line134"></a>  this.colorPalette_.setSize(size);
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Gets the number of columns displayed.
<a name="line140"></a> * @return {goog.math.Size?} The size of the grid.
<a name="line141"></a> */
<a name="line142"></a>goog.ui.ColorPicker.prototype.getSize = function() {
<a name="line143"></a>  return this.colorPalette_ ? this.colorPalette_.getSize() : null;
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * Sets the number of columns.  Will throw an error after the picker has been
<a name="line149"></a> * rendered.
<a name="line150"></a> * @param {number} n The number of columns.
<a name="line151"></a> * @deprecated Use setSize.
<a name="line152"></a> */
<a name="line153"></a>goog.ui.ColorPicker.prototype.setColumnCount = function(n) {
<a name="line154"></a>  this.setSize(n);
<a name="line155"></a>};
<a name="line156"></a>
<a name="line157"></a>
<a name="line158"></a>/**
<a name="line159"></a> * @return {number} The index of the color selected.
<a name="line160"></a> */
<a name="line161"></a>goog.ui.ColorPicker.prototype.getSelectedIndex = function() {
<a name="line162"></a>  return this.colorPalette_ ? this.colorPalette_.getSelectedIndex() : -1;
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Sets which color is selected. A value that is out-of-range means that no
<a name="line168"></a> * color is selected.
<a name="line169"></a> * @param {number} ind The index in this.colors_ of the selected color.
<a name="line170"></a> */
<a name="line171"></a>goog.ui.ColorPicker.prototype.setSelectedIndex = function(ind) {
<a name="line172"></a>  if (this.colorPalette_) {
<a name="line173"></a>    this.colorPalette_.setSelectedIndex(ind);
<a name="line174"></a>  }
<a name="line175"></a>};
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * Gets the color that is currently selected in this color picker.
<a name="line180"></a> * @return {string?} The hex string of the color selected, or null if no
<a name="line181"></a> *     color is selected.
<a name="line182"></a> */
<a name="line183"></a>goog.ui.ColorPicker.prototype.getSelectedColor = function() {
<a name="line184"></a>  return this.colorPalette_ ? this.colorPalette_.getSelectedColor() : null;
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * Sets which color is selected.  Noop if the color palette hasn&#39;t been created
<a name="line190"></a> * yet.
<a name="line191"></a> * @param {string} color The selected color.
<a name="line192"></a> */
<a name="line193"></a>goog.ui.ColorPicker.prototype.setSelectedColor = function(color) {
<a name="line194"></a>  // TODO: This will set the color in the first available palette that
<a name="line195"></a>  // contains it
<a name="line196"></a>  if (this.colorPalette_) {
<a name="line197"></a>    this.colorPalette_.setSelectedColor(color);
<a name="line198"></a>  }
<a name="line199"></a>};
<a name="line200"></a>
<a name="line201"></a>
<a name="line202"></a>/**
<a name="line203"></a> * Returns true if the component is focusable, false otherwise.  The default
<a name="line204"></a> * is true.  Focusable components always have a tab index and allocate a key
<a name="line205"></a> * handler to handle keyboard events while focused.
<a name="line206"></a> * @return {boolean} True iff the component is focusable.
<a name="line207"></a> */
<a name="line208"></a>goog.ui.ColorPicker.prototype.isFocusable = function() {
<a name="line209"></a>  return this.focusable_;
<a name="line210"></a>};
<a name="line211"></a>
<a name="line212"></a>
<a name="line213"></a>/**
<a name="line214"></a> * Sets whether the component is focusable.  The default is true.
<a name="line215"></a> * Focusable components always have a tab index and allocate a key handler to
<a name="line216"></a> * handle keyboard events while focused.
<a name="line217"></a> * @param {boolean} focusable True iff the component is focusable.
<a name="line218"></a> */
<a name="line219"></a>goog.ui.ColorPicker.prototype.setFocusable = function(focusable) {
<a name="line220"></a>  this.focusable_ = focusable;
<a name="line221"></a>  if (this.colorPalette_) {
<a name="line222"></a>    this.colorPalette_.setSupportedState(goog.ui.Component.State.FOCUSED,
<a name="line223"></a>        focusable);
<a name="line224"></a>  }
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/**
<a name="line229"></a> * ColorPickers cannot be used to decorate pre-existing html, since the
<a name="line230"></a> * structure they build is fairly complicated.
<a name="line231"></a> * @param {Element} element Element to decorate.
<a name="line232"></a> * @return {boolean} Returns always false.
<a name="line233"></a> */
<a name="line234"></a>goog.ui.ColorPicker.prototype.canDecorate = function(element) {
<a name="line235"></a>  return false;
<a name="line236"></a>};
<a name="line237"></a>
<a name="line238"></a>
<a name="line239"></a>/**
<a name="line240"></a> * Renders the color picker inside the provided element. This will override the
<a name="line241"></a> * current content of the element.
<a name="line242"></a> * @override
<a name="line243"></a> */
<a name="line244"></a>goog.ui.ColorPicker.prototype.enterDocument = function() {
<a name="line245"></a>  goog.ui.ColorPicker.superClass_.enterDocument.call(this);
<a name="line246"></a>  if (this.colorPalette_) {
<a name="line247"></a>    this.colorPalette_.render(this.getElement());
<a name="line248"></a>  }
<a name="line249"></a>  this.getElement().unselectable = &#39;on&#39;;
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/** @inheritDoc */
<a name="line254"></a>goog.ui.ColorPicker.prototype.disposeInternal = function() {
<a name="line255"></a>  goog.ui.ColorPicker.superClass_.disposeInternal.call(this);
<a name="line256"></a>  if (this.colorPalette_) {
<a name="line257"></a>    this.colorPalette_.dispose();
<a name="line258"></a>    this.colorPalette_ = null;
<a name="line259"></a>  }
<a name="line260"></a>};
<a name="line261"></a>
<a name="line262"></a>
<a name="line263"></a>/**
<a name="line264"></a> * Sets the focus to the color picker&#39;s palette.
<a name="line265"></a> */
<a name="line266"></a>goog.ui.ColorPicker.prototype.focus = function() {
<a name="line267"></a>  if (this.colorPalette_) {
<a name="line268"></a>    this.colorPalette_.getElement().focus();
<a name="line269"></a>  }
<a name="line270"></a>};
<a name="line271"></a>
<a name="line272"></a>
<a name="line273"></a>/**
<a name="line274"></a> * Handles actions from the color palette.
<a name="line275"></a> *
<a name="line276"></a> * @param {goog.events.Event} e The event.
<a name="line277"></a> * @private
<a name="line278"></a> */
<a name="line279"></a>goog.ui.ColorPicker.prototype.onColorPaletteAction_ = function(e) {
<a name="line280"></a>  e.stopPropagation();
<a name="line281"></a>  this.dispatchEvent(goog.ui.ColorPicker.EventType.CHANGE);
<a name="line282"></a>};
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>/**
<a name="line286"></a> * Create a color palette for the color picker.
<a name="line287"></a> * @param {Array.&lt;string&gt;} colors Array of colors.
<a name="line288"></a> * @private
<a name="line289"></a> */
<a name="line290"></a>goog.ui.ColorPicker.prototype.createColorPalette_ = function(colors) {
<a name="line291"></a>  // TODO: The color picker should eventually just contain a number of
<a name="line292"></a>  // palettes and manage the interactions between them.  This will go away then.
<a name="line293"></a>  var cp = new goog.ui.ColorPalette(colors, null, this.getDomHelper());
<a name="line294"></a>  cp.setSize(goog.ui.ColorPicker.DEFAULT_NUM_COLS);
<a name="line295"></a>  cp.setSupportedState(goog.ui.Component.State.FOCUSED, this.focusable_);
<a name="line296"></a>  // TODO: Use addChild(cp, true) and remove calls to render.
<a name="line297"></a>  this.addChild(cp);
<a name="line298"></a>  this.colorPalette_ = cp;
<a name="line299"></a>  if (this.isInDocument()) {
<a name="line300"></a>    this.colorPalette_.render(this.getElement());
<a name="line301"></a>  }
<a name="line302"></a>};
<a name="line303"></a>
<a name="line304"></a>
<a name="line305"></a>/**
<a name="line306"></a> * Returns an unrendered instance of the color picker.  The colors and layout
<a name="line307"></a> * are a simple color grid, the same as the old Gmail color picker.
<a name="line308"></a> * @param {goog.dom.DomHelper} opt_domHelper Optional DOM helper.
<a name="line309"></a> * @return {goog.ui.ColorPicker} The unrendered instance.
<a name="line310"></a> */
<a name="line311"></a>goog.ui.ColorPicker.createSimpleColorGrid = function(opt_domHelper) {
<a name="line312"></a>  var cp = new goog.ui.ColorPicker(opt_domHelper);
<a name="line313"></a>  cp.setSize(7);
<a name="line314"></a>  cp.setColors(goog.ui.ColorPicker.SIMPLE_GRID_COLORS);
<a name="line315"></a>  return cp;
<a name="line316"></a>};
<a name="line317"></a>
<a name="line318"></a>
<a name="line319"></a>/**
<a name="line320"></a> * Array of colors for a 7-cell wide simple-grid color picker.
<a name="line321"></a> * @type {Array.&lt;string&gt;}
<a name="line322"></a> */
<a name="line323"></a>goog.ui.ColorPicker.SIMPLE_GRID_COLORS = [
<a name="line324"></a>  // grays
<a name="line325"></a>  &#39;#ffffff&#39;, &#39;#cccccc&#39;, &#39;#c0c0c0&#39;, &#39;#999999&#39;, &#39;#666666&#39;, &#39;#333333&#39;, &#39;#000000&#39;,
<a name="line326"></a>  // reds
<a name="line327"></a>  &#39;#ffcccc&#39;, &#39;#ff6666&#39;, &#39;#ff0000&#39;, &#39;#cc0000&#39;, &#39;#990000&#39;, &#39;#660000&#39;, &#39;#330000&#39;,
<a name="line328"></a>  // oranges
<a name="line329"></a>  &#39;#ffcc99&#39;, &#39;#ff9966&#39;, &#39;#ff9900&#39;, &#39;#ff6600&#39;, &#39;#cc6600&#39;, &#39;#993300&#39;, &#39;#663300&#39;,
<a name="line330"></a>  // yellows
<a name="line331"></a>  &#39;#ffff99&#39;, &#39;#ffff66&#39;, &#39;#ffcc66&#39;, &#39;#ffcc33&#39;, &#39;#cc9933&#39;, &#39;#996633&#39;, &#39;#663333&#39;,
<a name="line332"></a>  // olives
<a name="line333"></a>  &#39;#ffffcc&#39;, &#39;#ffff33&#39;, &#39;#ffff00&#39;, &#39;#ffcc00&#39;, &#39;#999900&#39;, &#39;#666600&#39;, &#39;#333300&#39;,
<a name="line334"></a>  // greens
<a name="line335"></a>  &#39;#99ff99&#39;, &#39;#66ff99&#39;, &#39;#33ff33&#39;, &#39;#33cc00&#39;, &#39;#009900&#39;, &#39;#006600&#39;, &#39;#003300&#39;,
<a name="line336"></a>  // turquoises
<a name="line337"></a>  &#39;#99ffff&#39;, &#39;#33ffff&#39;, &#39;#66cccc&#39;, &#39;#00cccc&#39;, &#39;#339999&#39;, &#39;#336666&#39;, &#39;#003333&#39;,
<a name="line338"></a>  // blues
<a name="line339"></a>  &#39;#ccffff&#39;, &#39;#66ffff&#39;, &#39;#33ccff&#39;, &#39;#3366ff&#39;, &#39;#3333ff&#39;, &#39;#000099&#39;, &#39;#000066&#39;,
<a name="line340"></a>  // purples
<a name="line341"></a>  &#39;#ccccff&#39;, &#39;#9999ff&#39;, &#39;#6666cc&#39;, &#39;#6633ff&#39;, &#39;#6600cc&#39;, &#39;#333399&#39;, &#39;#330099&#39;,
<a name="line342"></a>  // violets
<a name="line343"></a>  &#39;#ffccff&#39;, &#39;#ff99ff&#39;, &#39;#cc66cc&#39;, &#39;#cc33cc&#39;, &#39;#993399&#39;, &#39;#663366&#39;, &#39;#330033&#39;
<a name="line344"></a>];
</pre>


</body>
</html>
